#region --- License & Copyright Notice ---

/*

ID3.NET ID3 tag implementation library

Copyright (c) 2007 Jeevan James
All rights reserved.

The contents of this file are made available under the terms of the
Eclipse Public License v1.0 (the "License") which accompanies this
distribution, and is available at the following URL:
http://opensource.org/licenses/eclipse-1.0.txt

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

By using this software in any fashion, you are agreeing to be bound by the
terms of the License.

*/

#endregion

using System;
using System.IO;

namespace Id3.Net.Test.ExtractId3
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            if (args.Length < 1)
                throw new ApplicationException("Requires at least one parameter");

            string sourceFile = args[0];
            if (!File.Exists(sourceFile))
                throw new ApplicationException("Cannot find source file");

            string destinationFile;
            if (args.Length > 1)
            {
                destinationFile = args[1];
                string destinationDirectory = Path.GetDirectoryName(destinationFile);
                if (!Directory.Exists(destinationDirectory))
                    throw new ApplicationException("Destination directory does not exist");
            }
            else
                destinationFile = Path.ChangeExtension(sourceFile, "id3");

            byte[] tagBytes = Mp3File.GetTagBytes(sourceFile, 2, 3);
            if (tagBytes == null)
                throw new ApplicationException("Could not find tag for ID3 2.3");
            using (FileStream destination = new FileStream(destinationFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                destination.Write(tagBytes, 0, tagBytes.Length);
                destination.Flush();
            }

            Console.WriteLine("Tag data successfully written");
            Console.ReadKey();
        }
    }
}
